package image;

import java.awt.Point;
import java.util.Collection;

import javafx.scene.Node;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.shape.Polygon;

/**
 * Represents a triangle
 * @author Etienne Bruines
 * @author Bas Dobbelsteen
 */
public class Triangle extends Layer 
{
	private final javafx.scene.shape.Polygon polygon;
	
	private int width, height, x, y;
	
	public Triangle(String t, Paint foreColor, Paint backColor, double strokeWidth, Collection<Point> points) 
	{
		super(t);
		polygon = new Polygon();
		for (Point p : points)
		{
			polygon.getPoints().addAll(p.getX(), p.getY());
		}
		polygon.setStroke(foreColor);
		polygon.setStrokeWidth(strokeWidth);
		polygon.setFill(backColor);
		
		super.setBinding();
	}

	@Override public Node getContent() 
	{
		return polygon;
	}

	@Override public int getWidth()
	{
		if (width == 0)
			width = (int) ( Math.max(Math.max(polygon.getPoints().get(0), polygon.getPoints().get(2)), polygon.getPoints().get(4))
					-  Math.min(Math.min(polygon.getPoints().get(0), polygon.getPoints().get(2)), polygon.getPoints().get(4)));
		return width;
	}

	@Override public int getHeight()
	{
		if (height == 0)
			height = (int) ( Math.max(Math.max(polygon.getPoints().get(1), polygon.getPoints().get(3)), polygon.getPoints().get(5))
					-  Math.min(Math.min(polygon.getPoints().get(1), polygon.getPoints().get(3)), polygon.getPoints().get(5)));
		return height;
	}

	@Override public int getX()
	{
		if (x == 0)
			x = (int) Math.min(Math.min(polygon.getPoints().get(0), polygon.getPoints().get(2)), polygon.getPoints().get(4));
		return x;
	}

	@Override public int getY()
	{
		if (y == 0)
			y = (int) Math.min(Math.min(polygon.getPoints().get(1), polygon.getPoints().get(3)), polygon.getPoints().get(5));
		return y;
	}

	@Override public void setBorder(Color c)
	{
		this.polygon.setStroke(c);
	}

	@Override public void setBackground(Color c)
	{
		this.polygon.setFill(c);
	}

	@Override public void setStroke(double width)
	{
		this.polygon.setStrokeWidth(width);
	}

}
